﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Derbster.Domain;

namespace Derbster.Mock
{
    /// <summary>
    /// This is an in-memory version of repository. Note, we don't use the singleton pattern here. 
    /// Instead, we count on Unity to control the lifecycle of these objects. By default, we use
    /// a container-controlled lifecycle, so only one instance of a repository is returned per container.
    /// </summary>
    public class MockSkaterRepository : ISkaterRepository
    {
        private readonly ObservableCollection<Skater> _all;

        public MockSkaterRepository()
        {
            _all = new ObservableCollection<Skater>();
        }

        public Skater Create(string name, string number)
        {
            Skater newSkater = new Skater(name,number);

            newSkater.SkaterId = _all.Count + 1;
            _all.Add(newSkater);

            return newSkater;
        }

        public Skater FindById(int id)
        {
            return _all.SingleOrDefault(m => m.SkaterId == id);
        }

        public Skater FindByName(string name)
        {
            return _all.SingleOrDefault(m => m.Name.ToLower() == name.ToLower());
        }

        public Skater Update(Skater official)
        {
            return official;
        }

        public void Delete(Skater official)
        {
            _all.Remove(official);
        }

        public void Dispose()
        {
            return;
        }

        public IEnumerator<Skater> GetEnumerator()
        {
            return _all.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public Expression Expression
        {
            get
            {
                return _all.AsQueryable().Expression;
            }
        }

        public Type ElementType
        {
            get
            {
                return _all.AsQueryable().ElementType;
            }
        }

        public IQueryProvider Provider
        {
            get
            {
                return _all.AsQueryable().Provider;
            }
        }
    }
}
